﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Logic;
using TrainingsApp.Model;

namespace TrainingsApp.IPADApp.Permission
{
    public partial class UserRole : PageBase
    {
     

        private static List<T_ROLE_Entity> RoleList = null;
        private static List<TS_USER_ROLE_Entity> UserRoleList = null;
        public int UserId
        {
            get { return int.Parse(Request.QueryString["BizCode"]); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lbUser.Text = Server.UrlDecode(Request.QueryString["name"]);
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                this.cklRole.Items.Clear();

                RoleList = Sys_Permission_Logic.T_Role_GetAllRole();
                this.cklRole.DataSource = RoleList;
                this.cklRole.DataBind();

                UserRoleList = Sys_Permission_Logic.T_USER_Role_ListByUserId(UserId);
                for (int i = 0; i < UserRoleList.Count; i++)
                {
                    ListItem item = this.cklRole.Items.FindByValue(UserRoleList[i].ROLEID.ToString());
                    item.Selected = true;
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<TS_USER_ROLE_Entity> list = new List<TS_USER_ROLE_Entity>();
            decimal val = 0;
            for (int i = 0; i < this.cklRole.Items.Count; i++)
            {
                if (this.cklRole.Items[i].Selected)
                {
                    list.Add(new TS_USER_ROLE_Entity() { USERID = UserId, ROLEID = int.Parse(this.cklRole.Items[i].Value) });
                }
            }

            try
            {
                Sys_Permission_Logic.T_USER_Role_Update(UserId, list);

                MessageBox.Show("操作成功！", MessageBoxIcon.Sucess);
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(UrlReferrer);
        }
    }
}